/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.authentication.domain.AuthProvider;
import cn.ibizlab.core.authentication.service.AuthProviderService;
import cn.ibizlab.core.authentication.filter.AuthProviderSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[AuthProvider] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractAuthProviderResource {

    @Autowired
    public AuthProviderService authProviderService;

    @Autowired
    @Lazy
    public AuthProviderDTOMapping authProviderDtoMapping;

    @Autowired
    @Lazy
    public AuthProviderFilterDTOMapping authProviderFilterDtoMapping;


    /**
    * 创建Create 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthProviderDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"统一认证/外部认证" },  notes = "AuthProvider-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Create-all') or hasPermission(this.authProviderDtoMapping.toDomain(#dto),'uaa-AuthProvider-Create')")
    @PostMapping(value = {"auth_providers","auth_providers"})
    public ResponseEntity<AuthProviderDTO> create
            (@Validated @RequestBody AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        authProviderService.create(domain);
        AuthProvider rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 统一认证/外部认证
    * 
    *
    * @param id id
    * @return ResponseEntity<AuthProviderDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"统一认证/外部认证" },  notes = "AuthProvider-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Get-all') or hasPermission(this.authProviderDtoMapping.toDomain(returnObject.body),'uaa-AuthProvider-Get')")
    @GetMapping("auth_providers/{id}")
    public ResponseEntity<AuthProviderDTO> getById
            (@PathVariable("id") String id) {
        AuthProvider rt = authProviderService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 统一认证/外部认证
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"统一认证/外部认证" },  notes = "AuthProvider-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Remove-all') or hasPermission(this.authProviderService.get(#id),'uaa-AuthProvider-Remove')")
    @DeleteMapping("auth_providers/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = authProviderService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 统一认证/外部认证
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<AuthProviderDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"统一认证/外部认证" },  notes = "AuthProvider-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Update-all') or hasPermission(this.authProviderService.get(#id),'uaa-AuthProvider-Update')")
    @VersionCheck(entity = "authprovider" , versionfield = "updateTime")
    @PutMapping("auth_providers/{id}")
    public ResponseEntity<AuthProviderDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        domain.setId(id);
        authProviderService.update(domain);
        AuthProvider rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * All 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<AuthProviderDTO>>
    */
    @ApiOperation(value = "All", tags = {"统一认证/外部认证" },  notes = "AuthProvider-All ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-All-all') or hasPermission(this.authProviderDtoMapping.toDomain(#dto),'uaa-AuthProvider-All')")
    @PostMapping("auth_providers/all")
    public ResponseEntity<List<AuthProviderDTO>> all
            (@Validated @RequestBody AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        List<AuthProvider> rt = authProviderService.all(domain);
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * Cache_evict 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthProviderDTO>
    */
    @ApiOperation(value = "Cache_evict", tags = {"统一认证/外部认证" },  notes = "AuthProvider-Cache_evict ")
    @PostMapping("auth_providers/cache_evict")
    public ResponseEntity<AuthProviderDTO> cacheEvict
            (@Validated @RequestBody AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        AuthProvider rt = authProviderService.cacheEvict(domain);
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"统一认证/外部认证" },  notes = "AuthProvider-CheckKey ")
    @PostMapping("auth_providers/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        Integer rt = authProviderService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthProviderDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"统一认证/外部认证" },  notes = "AuthProvider-GetDraft ")
    @GetMapping("auth_providers/getdraft")
    public ResponseEntity<AuthProviderDTO> getDraft
            (@SpringQueryMap AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        AuthProvider rt = authProviderService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthProviderDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"统一认证/外部认证" },  notes = "AuthProvider-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Save-all') or hasPermission(this.authProviderDtoMapping.toDomain(#dto),'uaa-AuthProvider-Save')")
    @PostMapping("auth_providers/save")
    public ResponseEntity<AuthProviderDTO> save
            (@Validated @RequestBody AuthProviderDTO dto) {
        AuthProvider domain = authProviderDtoMapping.toDomain(dto);
        authProviderService.save(domain);
        AuthProvider rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(authProviderDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 统一认证/外部认证
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<AuthProviderDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"统一认证/外部认证" },  notes = "AuthProvider-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-FetchDefault-all') or hasPermission(#dto,'uaa-AuthProvider-FetchDefault')")
    @PostMapping("auth_providers/fetchdefault")
    public ResponseEntity<List<AuthProviderDTO>> fetchDefault
            (@Validated @RequestBody AuthProviderFilterDTO dto) {
        AuthProviderSearchContext context = authProviderFilterDtoMapping.toDomain(dto);
        Page<AuthProvider> domains = authProviderService.searchDefault(context) ;
        List<AuthProviderDTO> list = authProviderDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建统一认证/外部认证
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Create-all')")
    @ApiOperation(value = "批量新建统一认证/外部认证", tags = {"统一认证/外部认证" },  notes = "批量新建统一认证/外部认证")
	@PostMapping("auth_providers/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<AuthProviderDTO> dtos) {
        authProviderService.createBatch(authProviderDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除统一认证/外部认证
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Remove-all')")
    @ApiOperation(value = "批量删除统一认证/外部认证", tags = {"统一认证/外部认证" },  notes = "批量删除统一认证/外部认证")
	@DeleteMapping("auth_providers/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        authProviderService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新统一认证/外部认证
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Update-all')")
    @ApiOperation(value = "批量更新统一认证/外部认证", tags = {"统一认证/外部认证" },  notes = "批量更新统一认证/外部认证")
	@PutMapping("auth_providers/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<AuthProviderDTO> dtos) {
        authProviderService.updateBatch(authProviderDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存统一认证/外部认证
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Save-all')")
    @ApiOperation(value = "批量保存统一认证/外部认证", tags = {"统一认证/外部认证" },  notes = "批量保存统一认证/外部认证")
	@PostMapping("auth_providers/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<AuthProviderDTO> dtos) {
        authProviderService.saveBatch(authProviderDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入统一认证/外部认证
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthProvider-Save-all')")
    @ApiOperation(value = "批量导入统一认证/外部认证", tags = {"统一认证/外部认证" },  notes = "批量导入统一认证/外部认证")
	@PostMapping("auth_providers/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<AuthProviderDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(authProviderService.importData(config,ignoreError,authProviderDtoMapping.toDomain(dtos)));
    }

}
